package com.pro.common.login.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.pro.common.api.CommonConst;
import com.pro.common.api.model.ILoginInfo;
import com.pro.common.login.model.request.RegisterRequest;
import com.pro.common.login.service.BaseLoginInfoService;
import com.pro.common.api.sitecontent.intf.AuthRoleFeignClient;
import com.pro.common.jwt.AccessTokenJwt;
import com.pro.common.login.service.CommonLoginService;
import io.swagger.v3.oas.annotations.Parameter;
import com.pro.common.api.model.request.LoginRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "统一登录注册接口")
@RestController
@RequestMapping("/commonLogin")
public class CommonLoginController {

    @Autowired
    private CommonLoginService commonLoginService;
    @Autowired
    private BaseLoginInfoService loginInfoService;
    @Autowired
    private AuthRoleFeignClient authRoleFeignClient;

    @ApiOperationSupport(order = 1)
    @Operation(summary = "注册")
    @PostMapping("/register")
    public AccessTokenJwt register(HttpServletRequest request, HttpServletResponse response, @Parameter(description = "用户基础信息") @RequestBody RegisterRequest registerRequest) {
        String captchaInner = (String) request.getSession().getAttribute(CommonConst.Str.SYSTEM_CAPTCHA);
        AccessTokenJwt token = commonLoginService.register(request, captchaInner, registerRequest.getRequestBody() ,registerRequest.getSmsCode());
        response.setHeader("Authorization", token.getAccessToken());
        response.setHeader("Access-Control-Expose-Headers", "Authorization");
        return token;
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "登录")
    @PostMapping("/login")
    public AccessTokenJwt login(HttpServletResponse response, @RequestBody LoginRequest request) {
        // 返回token
        AccessTokenJwt token = commonLoginService.login(request);
        response.setHeader("Authorization", token.getAccessToken());
        response.setHeader("Access-Control-Expose-Headers", "Authorization");
        return token;
    }

    @ApiOperationSupport(order = 5)
    @Operation(summary = "查询是否开启谷歌秘钥登录")
    @GetMapping("/getGoogleAuthOpen")
    public Boolean getGoogleAuthOpen(LoginRequest loginRequest) {
        return commonLoginService.getGoogleAuthOpen(loginRequest);
    }


    @ApiOperationSupport(order = 6)
    @Operation(summary = "检查是否登录")
    @GetMapping("/validate")
    public Boolean validate(@Parameter(hidden = true) ILoginInfo loginInfo) {
        return loginInfoService.getSysRole().equals(loginInfo.getSysRole());
//        return !EnumSysRole.ANONYMOUS.equals(loginInfo.getSysRole());
//        return null != tokenService.getIdByToken(request.getHeader(TokenService.tokenKey)));
    }

    @ApiOperationSupport(order = 6)
    @Operation(summary = "确保活跃帐号")
    @GetMapping("/getActivity")
    public void getActivity(HttpServletRequest request) {
        commonLoginService.getActivity(request);
    }

}
